//
//  AskQuestionVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/10/12.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "AskQuestionVC.h"
#import "SelectAndShowPhotoView.h"
#import <IQKeyboardManager.h>
#import <IQTextView.h>

@interface AskQuestionVC ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//@property (weak, nonatomic) IBOutlet UIImageView *guideHeadImageView;
//@property (weak, nonatomic) IBOutlet UIImageView *guideVipImageView;
//@property (weak, nonatomic) IBOutlet UILabel *guideNameLabel;
@property (weak, nonatomic) IBOutlet IQTextView *contentTextView;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (unsafe_unretained, nonatomic) IBOutlet SelectAndShowPhotoView *selectView;
//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pageTop;

@property (nonatomic) BOOL hasTap;

@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewCenterY;

@end

@implementation AskQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = MYLocalizedString(@"SITEPLAY_SUBMIT_QUESTION", nil);
    self.automaticallyAdjustsScrollViewInsets = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    self.titleLabel.text = MYLocalizedString(@"SITEPLAY_SUBMIT_QUESTION", nil);
    //    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    
    self.selectView.maxCount = 3;
    self.selectView.superVC = self;
    
    self.contentTextView.placeholder =MYLocalizedString(@"SITEPLAY_QUESTION_TIPS", nil);
    self.contentTextView.textColor = kContentColor;
    
    //    [self rightBarButton:MYLocalizedString(@"PUBLIC_COMMENT_COMMIT", nil) color:[UIColor blackColor] selector:@selector(commitInfor)];
    
    self.contentTextView.layer.borderWidth = 1.0;
    self.contentTextView.layer.borderColor = kBGColor.CGColor;
//    self.pageTop.constant = kNavigationBarHeight + 16;
//
//    self.titleLabel.text = self.lineModel.linename;
//    [self.guideHeadImageView setImageURL:[NSURL URLWithString:self.lineModel.guide.headimg] placeholderImage:[UIImage imageNamed:@"placeholder_squ"]];
//    self.guideVipImageView.hidden = !self.lineModel.guide.vip;
//    self.guideNameLabel.text = self.lineModel.guide.realname;
    
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.57];
    
    self.navigationController.navigationBar.hidden = YES;
    
    self.bgView.layer.cornerRadius = 5;
    self.bgView.clipsToBounds = YES;
    
    self.submitBtn.layer.cornerRadius = 5;
    self.submitBtn.clipsToBounds = YES;
    self.bgViewCenterY.constant =  kScreenHeight/2;
    [self.submitBtn setTitle:MYLocalizedString(@"PUBLIC_COMMENT_COMMIT", nil) forState:UIControlStateNormal];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.customBarStyle = UIStatusBarStyleDefault;
}

- (void)close {
    
    if (self.contentTextView.isFirstResponder) {
        
        [self.contentTextView endEditing:YES];
    }else {
        [self dismissViewControllerAnimated:NO completion:nil];
        self.customBarStyle = UIStatusBarStyleDefault;
    }
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self appearAnimation];
}
- (IBAction)submitBtnClicked:(UIButton *)sender {
    
    [self commitInfor];
}


- (void)commitInfor {
    // 提交
    
    if (self.hasTap) {
        
        return;
    }
    
    self.hasTap = YES;
    
    NSString * str = self.contentTextView.text;
    
    NSString * result = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (result.length <= 0) {
        
        [IGUtil showToast:MYLocalizedString(@"SITEPLAY_COMMENT_EMPTY", nil)];
        self.hasTap = NO;
        return;
    }
    
    if (self.contentTextView.isFirstResponder) {
        [self.contentTextView resignFirstResponder];
    }
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    @weakify(self);
    
    [NetAPIManager qaWithLineid:self.lineModel.lineid question:self.contentTextView.text images:self.selectView.dataSource Block:^(id data, NSError *error) {
        @strongify(self);
        
        self.hasTap = NO;
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        if ([self.delegate respondsToSelector:@selector(AskQuestuibDone)]) {
            [self.delegate AskQuestuibDone];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        if (!error) {
            [IGAlertView alertWithTitle:MYLocalizedString(@"SITEPLAY_TIP_TITLE", nil) message:MYLocalizedString(@"SITEPLAY_TIP_CONTENT", nil) cancelButtonTitle:MYLocalizedString(@"PUBLIC_OK", nil) commitBtn:nil commit:nil cancel:^{
                
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
        
    }];
}

- (void)setupData {
    
//    self.titleLabel.text = self.lineModel.linename;
//    [self.guideVipImageView setImageURL:[NSURL URLWithString:self.lineModel.guide.headimg] placeholderImage:[UIImage imageNamed:@"default"]];
//    self.guideNameLabel.text = self.lineModel.guide.realname;
//    self.guideVipImageView.hidden = !self.lineModel.guide.vip;
}

#pragma mark - UITextViewDelegate
#define MAX_STARWORDS_LENGTH 100

- (void)textViewDidChange:(UITextView *)textView {
    
    NSString * string  =  textView.text;
    
    //获取高亮部分
    
    UITextRange * seletedRange = [textView markedTextRange];
    
    UITextPosition * position = [textView positionFromPosition:seletedRange.start offset:0];
    
    // 没有高亮选择的字， 就对其进行字数统计和限制
    
    if (!position) {
        
        if (string.length > MAX_STARWORDS_LENGTH) {
            
            NSRange rangeIndex = [string rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1) {
                
                textView.text = [string substringToIndex:MAX_STARWORDS_LENGTH];
            }else {
                
                NSRange rangeRange = [string rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                
                textView.text = [string substringWithRange:rangeRange];
            }
        }
    }
    int length =(int)(MAX_STARWORDS_LENGTH - textView.text.length);
    
    if (length >=0) {
        
        self.countLabel.text = @(MAX_STARWORDS_LENGTH - textView.text.length).stringValue;
    }
    
}

- (void)appearAnimation {
    self.bgViewCenterY.constant = 0;
    @weakify(self);
    [UIView animateWithDuration:0.25 animations:^{
        @strongify(self);
        [self.view layoutIfNeeded];
    }];
    
}

//- (void)disappearAnimation {
//    self.bgViewCenterY.constant = kScreenHeight/2;
//
//    [UIView animateWithDuration:1 animations:^{
//        [self.view layoutIfNeeded];
//    }completion:^(BOOL finished) {
//
//    }];
//
//}

#pragma mark --UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.bgView]) {
        return NO;
    }
    
    return YES;
}

@end


